package com.ymatou.c2clivebizer.testcase;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.cookie.CookieIdentityComparator;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.c2clivebizer.parameter.LiveFilterBean;
import com.ymatou.c2clivebizer.service.LiveFilterCall;
import com.ymt.base.BaseBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P3;

public class Ts_LiveFilter {
	private static LiveFilterBean lfBean;
	private static LiveFilterCall lfCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商品Topic详情列表接口");
	}

	@Before
	public void caseUp() {
		lfBean = new LiveFilterBean();
		lfCall = new LiveFilterCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(lfCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_LiveFilter_001() {
		Logger.start(true,"直播筛选级联接口-返回状态验证");
		try {
			
			//参数赋值
		 String	Module= "live_filter"; 
		 int isecondcategoryid = -1;
		 int ibrandid=13290;
		 Date date=new Date();
	     DateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
	     String time=format.format(date);
		 String userid ="8697222";
		 String cookie = "1D2F44C7-1E96-45B8-9A48-328849C5FF20";
		 
		 String filter = "and(or(term(isecondcategoryid,"+isecondcategoryid+")),or(term(ibrandid,"+ibrandid+")))";
		 String info = "timenow:"+time+",userid:"+userid+",cookieid:"+cookie;
		 
			lfBean.setModule("mainhome"); //表示是主页来的请求
			lfBean.setFilter(filter);
			lfBean.setInfo(info);
		
			lfCall.setData(lfBean);
			lfCall.callService();	
			Logger.verifyEquals("OK", lfCall.getStatus(), "接口返回状态ok");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_LiveFilter_002() {
		Logger.start(true,"直播筛选级联接口-检查返回聚合项");
		try {
			
			//参数赋值
		 String	Module= "live_filter"; 
		 int isecondcategoryid = -1;
		 int ibrandid=13290;
		 Date date=new Date();
	     DateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
	     String time=format.format(date);
		 String userid ="8697222";
		 String cookie = "1D2F44C7-1E96-45B8-9A48-328849C5FF20";
		 
		 String filter = "and(or(term(isecondcategoryid,"+isecondcategoryid+")),or(term(ibrandid,"+ibrandid+")))";
		 String info = "timenow:"+time+",userid:"+userid+",cookieid:"+cookie;
		 
			lfBean.setModule("mainhome"); //表示是主页来的请求
			lfBean.setFilter(filter);
			lfBean.setInfo(info);
		
			lfCall.setData(lfBean);
			lfCall.callService();	
		 JSONObject stats =	lfCall.getStats();
		
		Logger.verifyEquals(true, stats.has("ibrandid"), "聚合结果中是否包含ibrandid");
		Logger.verifyEquals(true, stats.has("isecondcategoryid"), "聚合结果中是否包含isecondcategoryid");
		Logger.verifyEquals(true, stats.has("countryId"), "聚合结果中是否包含countryId");	
		Logger.verifyEquals(true, stats.has("liveActivityId"), "聚合结果中是否包含liveActivityId");	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_LiveFilter_003() {
		Logger.start(true,"直播筛选级联接口-检查返回筛选结果");
		try {
			
			//参数赋值
		 String	Module= "live_filter"; 
		 String countryId = "50"; //过滤条件
		 String ibrandid="13290";  //过滤条件
		 Date date=new Date();
	     DateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
	     String time=format.format(date);
		 String userid ="8697222";
		 String cookie = "1D2F44C7-1E96-45B8-9A48-328849C5FF20";
		 
		 String filter = "and(or(term(countryId,"+countryId+")),or(term(ibrandid,"+ibrandid+")))";
		 String info = "timenow:"+time+",userid:"+userid+",cookieid:"+cookie;
		 
			lfBean.setModule("mainhome"); //表示是主页来的请求
			lfBean.setFilter(filter);
			lfBean.setInfo(info);
		
			lfCall.setData(lfBean);
			lfCall.callService();	
		 JSONObject stats =	lfCall.getStats();
		
		JSONArray ibrandArray = new JSONArray(stats.get("ibrandid").toString());
		JSONObject bidArray = new JSONObject(ibrandArray.get(0).toString());
		
		JSONArray countryArray = new JSONArray(stats.get("countryId").toString());
		JSONObject cidArray = new JSONObject(countryArray.get(0).toString());
		
		Logger.verifyEquals(cidArray.get("id"), countryId, "验证返回countryId");
		Logger.verifyEquals(bidArray.get("id"), ibrandid, "验证返回ibrandid");
		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_LiveFilter_004() {
		Logger.start(false,"直播筛选级联接口-只满足其中部分过滤条件");
		try {
			
			//参数赋值
		 String	Module= "live_filter"; 
		 int countryId = 50; //过滤条件
		 int ibrandid=13290000;  //过滤条件
		 Date date=new Date();
	     DateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
	     String time=format.format(date);
		 String userid ="8697222";
		 String cookie = "1D2F44C7-1E96-45B8-9A48-328849C5FF20";
		 
		 String filter = "and(or(term(countryId,"+countryId+")),or(term(ibrandid,"+ibrandid+")))";
		 String info = "timenow:"+time+",userid:"+userid+",cookieid:"+cookie;
		 
			lfBean.setModule("mainhome"); //表示是主页来的请求
			lfBean.setFilter(filter);
			lfBean.setInfo(info);
		
			lfCall.setData(lfBean);
			lfCall.callService();	
		 JSONObject stats =	lfCall.getStats();
		
		 Logger.verifyEquals("OK", lfCall.getStatus(), "接口返回状态ok");
		 Logger.verifyEquals("[]", stats.getString("ibrandid"), "返回ibrandid对象为空");
		 Logger.verifyEquals("[]", stats.getString("isecondcategoryid"), "返回isecondcategoryid对象为空"); 
		 Logger.verifyEquals("[]", stats.getString("countryId"), "返回countryId对象为空");
		 Logger.verifyEquals("[]", stats.getString("liveActivityId"), "返回liveActivityId对象为空");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}
